Page({
  /**
   * 页面的初始数据
   */
  data: {
    allChecked: false, //是否全选
    goodslist: [], //商品列表
    orderInfo: {}, //订单信息
    reason: "", //退款原因
    result: [], //多选框点击
    fileList: [], //待上传图片地址
    price: 0, //退款总金额
  },
  initPage: function(option) {
    wx.hideShareMenu();
    console.log(this.data.goodslist)
    if (option.id) {
      this.setData({
        orderNumber: option.id
      })
    } else {
      util.msg("订单号错误")
    }
    this.httpGetOrderDetailr();
  },
  //点击复选框
  onChange(event) {
    this.setData({
      result: event.detail,
    });
    this.cprice()
  },
  //点击全选
  onAllChange(event) {
    console.log(event)
    if (event.detail) {
      let list = [];
      let i = 0;
      for (i; i < this.data.goodslist.length; i++) {
        list.push(i + "")
      }
      this.setData({
        allChecked: event.detail,
        result: list
      })
    } else {
      this.setData({
        allChecked: event.detail,
        result: []
      });
    }
    this.cprice()
  },

  //计算价格
  cprice() {
    let price = 0;
    let {
      result,
      goodslist
    } = this.data
    if (result.length > 0) {
      let i = 0;
      for (i; i < result.length; i++) {
        price = price + goodslist[result[i]].rawPrice
      }
    }
    this.setData({
      price: price
    })
  },
  //判断上传类型
  beforeRead(event) {
    const {
      file,
      callback = () => {}
    } = event.detail;
    console.log(event)
    //   console.log('before上传', file);
    //   if (file.type !== 'image') {
    //     callback(false);
    //     return;
    //   }
    callback(true);
  },
  //添加图片
  afterRead(event) {
    let list = [];
    list = this.data.fileList;
    list.push({
      url: event.detail.file.path
    })
    this.setData({
      fileList: list
    })
    console.log('afterRead', event.detail.file.path)
  },
  //删除选的图片
  deleteRead(event) {
    let list = [];
    list = this.data.fileList;
    list.splice(event.detail.index, 1)
    this.setData({
      fileList: list
    })
  },

  // 获取订单信息
  httpGetOrderDetailr() {
    return userHttp.getOrderDetailr({
      orderNumber: this.data.orderNumber
    }).then((res) => {
      this.setData({
        goodslist: res.goodsInfo,
        orderInfo: res,
      })
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }).catch(err => {
      util.msg("网络错误,请检查后重试!")
      wx.navigateBack({
        delta: 1
      })

    })
  },
  //输入的字发生改变时
  changeInput(e) {
    this.setData({
      reason: e.detail.value
    })
    console.log(e.detail.value)
  },
  //确认提交
  onSubmit() {
    let list = this.data.result
    let goods = this.data.goodslist
    if (list.length > 0) {
      let i = 0;
      let orderId = ""
      for (i; i < list.length; i++) {
        console.log(list[i])
        console.log(goods)

        orderId = orderId == '' ? goods[list[i]].unionOrderId: orderId+',' + goods[list[i]].unionOrderId
      }
      console.log(orderId)

      this.httpPartAfterSale(orderId)
    } else {
      util.msg("请选择要退款的商品")
    }
  },
  httpPartAfterSale(orderId) {
    userHttp.partAfterSale({
      unionOrderId: this.data.orderInfo.fatherOrderId,
      orderNumbers: orderId,
      reason: this.data.reason,
      vouchers: ''
    }).then((res) => {

    })
  }

});
